package com.lxmes.hr.service.impl;

import java.util.List;

import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.hr.mapper.NcHolidayManageMapper;
import com.lxmes.hr.domain.NcHolidayManage;
import com.lxmes.hr.service.INcHolidayManageService;
import javax.validation.Validator;

/**
 * 节日管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-17
 */
@Service
public class NcHolidayManageServiceImpl implements INcHolidayManageService
{
    @Autowired
    private NcHolidayManageMapper ncHolidayManageMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询节日管理
     *
     * @param id 节日管理主键
     * @return 节日管理
     */
    @Override
    public NcHolidayManage selectNcHolidayManageById(Long id)
    {
        return ncHolidayManageMapper.selectNcHolidayManageById(id);
    }

    /**
     * 查询节日管理列表
     *
     * @param ncHolidayManage 节日管理
     * @return 节日管理
     */
    @Override
    public List<NcHolidayManage> selectNcHolidayManageList(NcHolidayManage ncHolidayManage)
    {
        return ncHolidayManageMapper.selectNcHolidayManageList(ncHolidayManage);
    }

    /**
     * 新增节日管理
     *
     * @param ncHolidayManage 节日管理
     * @return 结果
     */
    @Override
    public int insertNcHolidayManage(NcHolidayManage ncHolidayManage)
    {
        ncHolidayManage.setCreateTime(DateUtils.getNowDate());
        if(Integer.parseInt(ncHolidayManage.getMonth())<10){
            ncHolidayManage.setMonth("0"+ncHolidayManage.getMonth());
        }
        if(Integer.parseInt(ncHolidayManage.getDay())<10){
            ncHolidayManage.setDay("0"+ncHolidayManage.getDay());
        }
        return ncHolidayManageMapper.insertNcHolidayManage(ncHolidayManage);
    }

    /**
     * 修改节日管理
     *
     * @param ncHolidayManage 节日管理
     * @return 结果
     */
    @Override
    public int updateNcHolidayManage(NcHolidayManage ncHolidayManage)
    {
        ncHolidayManage.setUpdateTime(DateUtils.getNowDate());
        return ncHolidayManageMapper.updateNcHolidayManage(ncHolidayManage);
    }

    /**
     * 批量删除节日管理
     *
     * @param ids 需要删除的节日管理主键
     * @return 结果
     */
    @Override
    public int deleteNcHolidayManageByIds(Long[] ids)
    {
        return ncHolidayManageMapper.deleteNcHolidayManageByIds(ids);
    }

    /**
     * 删除节日管理信息
     *
     * @param id 节日管理主键
     * @return 结果
     */
    @Override
    public int deleteNcHolidayManageById(Long id)
    {
        return ncHolidayManageMapper.deleteNcHolidayManageById(id);
    }
    @Override
    public String imporIteminfo(List<NcHolidayManage> ncHolidayManageList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(ncHolidayManageList) || ncHolidayManageList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcHolidayManage ncHolidayManage : ncHolidayManageList)
        {
            try
            {
                BeanValidators.validateWithException(validator, ncHolidayManage);
                ncHolidayManage.setCreateBy(operName);
                ncHolidayManage.setUpdateBy(operName);
                this.insertNcHolidayManage(ncHolidayManage);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
